package com.tigercel.xiangchengyun.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.HotVideoAdapter
import com.tigercel.xiangchengyun.adapter.NewsBannerAdapter
import com.tigercel.xiangchengyun.entity.Banner
import com.tigercel.xiangchengyun.entity.HotVideoEntity
import com.tigercel.xiangchengyun.ui.activities.MoreViewsActivity
import com.tigercel.xiangchengyun.ui.custom.lbanners.LMBanners
import com.tigercel.xiangchengyun.ui.custom.lbanners.transformer.TransitionEffect
import com.tigercel.xiangchengyun.utils.ExpandGridView
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import kotlinx.android.synthetic.main.fragment_video_test.*
import org.jetbrains.anko.support.v4.toast
import java.text.SimpleDateFormat
import com.tigercel.xiangchengyun.utils.Global
class VideoParticular2Fragment : Fragment() {
    companion object {

        fun newInstance(channelPosition: Int): VideoParticular2Fragment? {
            val fragment = VideoParticular2Fragment()
            val bundle = Bundle()
            bundle.putInt("channelPosition", channelPosition)
            fragment.arguments = bundle
            return fragment

        }

    }

    private var emptyView: View? = null
    private var containerView: View? = null
    private var banner: LMBanners<Any>? = null
    private var headBanner: View? = null
    private var channelPosition: Int = 0
    private var VideoList: MutableList<HotVideoEntity>? = null
    private var localDatas: PreferencesUtil? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_video_test, container, false)
        if (arguments != null) {
            channelPosition = arguments!!.getInt("channelPosition")
        }
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        headBanner = View.inflate(activity, R.layout.banners, null)
        banner = headBanner?.findViewById(R.id.banners)
        banner!!.setIndicatorBottomPadding(8)
        banner!!.setHoriZontalTransitionEffect(TransitionEffect.Default)
        localDatas = PreferencesUtil(activity)
        getContent("create")
        refresh_layout.isEnableLoadMore = false
        refresh_layout.autoRefresh()
        refresh_layout.setOnRefreshListener(OnRefreshListener {
            VideoList!!.clear()
            LiContiner.removeAllViews()
            getContent("")
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        banner!!.clearImageTimerTask()
    }

    override fun onResume() {
        super.onResume()
//        getContent("create")
    }

    private fun getContent(type: String) {

//        hotVideoList.clear()
//        tvvideoList.clear()
//        festivalvideoList.clear()
//        VideoList!!.clear()
        val devicesId = Settings.System.getString(activity!!.getContentResolver(), Settings.System.ANDROID_ID)
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        OkGo.get<String>(Global.baseUrl + "v1/channel/content")
                .tag(this)
                .params("channel_id", channelPosition)
                .params("device_code", devicesId)
                .params("user_id", Global.commonUserId)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val content = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        val data = JsonUtil.get_key_string("data", content!!)
                        if (status == "0") {

                            var banners1: MutableList<Banner>
                            banners1 = ArrayList()
                            banners1.clear()
                            val banners = JsonUtil.getList("banner", data)
                            if (banners.isNotEmpty()) {
                                if (type == "create") {
                                    LiContiner.addView(headBanner)
                                }
                                for (i in 0 until banners.size) {
                                    var img = JsonUtil.get_key_string("img", banners[i])
                                    var url = JsonUtil.get_key_string("url", banners[i])
                                    var name = JsonUtil.get_key_string("title", banners[i])
                                    var bannerBean = Banner(i, name, img, url)
                                    banners1.add(bannerBean)
                                }
                                banner!!.setAdapter(NewsBannerAdapter(activity), banners1 as List<Any>?)
                            }
                            val contentList = JsonUtil.getList("content", data)
                            if (activity != null) {

                                for (i in 0 until contentList.size) {
                                    val contentView: View? = View.inflate(activity, R.layout.hot_video, null)
                                    val name: TextView? = contentView!!.findViewById(R.id.video_title)
                                    val grid: ExpandGridView? = contentView!!.findViewById(R.id.hot_video_recycleview)
                                    val moreView: LinearLayout = contentView!!.findViewById(R.id.li_more)
                                    VideoList = ArrayList()
                                    var VideoAdapter: HotVideoAdapter? = null
                                    val channel_id = JsonUtil.get_key_string("channel_id", contentList[i])
                                    val name1 = JsonUtil.get_key_string("name", contentList[i])
                                    val model = JsonUtil.get_key_string("model", contentList[i])
                                    val prefix = JsonUtil.get_key_string("prefix", contentList[i])
                                    val show_type = JsonUtil.get_key_string("show_type", contentList[i])
                                    val realContent = JsonUtil.getList("content", contentList[i])
                                    name!!.text = name1
                                    moreView.setOnClickListener(View.OnClickListener {
                                        var intent = Intent()
                                        intent.putExtra("title", name!!.text)
                                        intent.putExtra("channel_id", channel_id)
                                        intent.setClass(activity, MoreViewsActivity::class.java)
                                        startActivity(intent)
                                    })
                                    for (k in 0 until realContent.size) {
                                        var content_id = JsonUtil.get_key_string("content_id", realContent[k])
                                        var title = JsonUtil.get_key_string("title", realContent[k])
                                        var thumb_img = JsonUtil.get_key_string("thumb_img", realContent[k])
                                        var url = JsonUtil.get_key_string("url", realContent[k])
                                        var pub_time = JsonUtil.get_key_string("pub_time", realContent[k]).toLong()
                                        var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                        VideoList!!.add(hotVideoEntity)
                                    }
                                    VideoAdapter = HotVideoAdapter(activity!!.applicationContext, VideoList!!)
                                    grid!!.adapter = VideoAdapter
//                                recyclerview.addHeaderView(contentView)
                                    LiContiner.addView(contentView)
                                }
                                refresh_layout.finishRefresh()
                            }

                        } else {
                            toast(getString(R.string.readFaile))
                        }
                    }

                })
    }
}
